package com.neutech.config;

import com.zaxxer.hikari.HikariDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
//使用注解加类里边的方法代替xml文件
@Configuration
//excludeFilters替代原来的排除
@ComponentScan(basePackages = {"com.neutech"} ,excludeFilters = {@ComponentScan.Filter(value = {Controller.class})})
@EnableTransactionManagement//开启事务注解
public class SpringConfig {
    @Bean
    public DataSource createDataSource(){
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName("com.mysql.ci.jdbc.Driver");
        dataSource.setJdbcUrl("");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        return  dataSource;
    }

    @Bean("sqlSessionFactory")
    public SqlSessionFactoryBean createSqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }
    //配置扫描器
    @Bean
    public MapperScannerConfigurer createMapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer =new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        mapperScannerConfigurer.setBasePackage("com.neutech.mapper");
        return mapperScannerConfigurer;
    }

    //配置事务器管理对象

}
